package com.bhs.sansonglogistics.ui.adapter;

import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.bean.DedicatedLineBean;
import com.bhs.sansonglogistics.bean.PriceBean;
import com.bhs.sansonglogistics.utils.LogUtils;
import com.bhs.sansonglogistics.utils.MyUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;

import java.util.List;

public class DedicatedLineAdapter extends BaseQuickAdapter<DedicatedLineBean, BaseViewHolder> {

    private final boolean isCompany;
    private int type;//1专线列表  2专线推广

    public DedicatedLineAdapter(int layoutResId) {
        super(layoutResId);
        isCompany = layoutResId == R.layout.item_dedicated_line2;
    }

    public DedicatedLineAdapter(int layoutResId, int type) {
        super(layoutResId);
        isCompany = layoutResId == R.layout.item_dedicated_line2;
        this.type = type;
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, DedicatedLineBean item) {
        int promoteType = Integer.parseInt(item.getPromote_type());
        String startCity = item.getStart_city();
        String endCity = item.getEnd_city();
        startCity = startCity.replace("市", "");
        endCity = endCity.replace("市", "");
        if (startCity.length() > 3) {
            startCity = startCity.substring(0, 3) + "...";
        }
        if (endCity.length() > 3) {
            endCity = endCity.substring(0, 3) + "...";
        }
        helper.setText(R.id.tv_line_name, startCity + "—" + endCity);
        if (!isCompany) {
            if (type == 1) {
                helper.addOnClickListener(R.id.btn_sold_out).addOnClickListener(R.id.btn_delete, R.id.btn_edit);
                helper.setText(R.id.btn_sold_out, item.getStatus() == 1 ? "下架" : "上架");
            } else {
                helper.addOnClickListener(R.id.btn_promotion);
            }
           // helper.setGone(R.id.btn_delete, item.getStatus() == 0 && type == 1);
            helper.setGone(R.id.btn_edit, item.getStatus() == 0 && type == 1);
            helper.setGone(R.id.btn_promotion, type == 2);
            helper.setGone(R.id.btn_sold_out, type == 1);
            helper.setVisible(R.id.tv_membership_days, (type == 2 && promoteType > 0)
                    || (!item.getMember_status().equals("0") && item.getStatus() == 1 && type == 1));
            if (type == 1) {
                if (!item.getMember_status().equals("0")) {
                    helper.setText(R.id.tv_membership_days, MyUtils.getEmberShipDays(type, promoteType, item.getMember_end_time()));
                }
            } else {
                if (promoteType > 0) {
                    helper.setText(R.id.tv_membership_days, MyUtils.getEmberShipDays(type, promoteType, item.getPromote_end_time()));
                }
            }
        }
        helper.setGone(R.id.tv_member, item.getMember_status().equals("1"));
        helper.setGone(R.id.tv_quality, item.getDeposit_status().equals("1"));
        helper.setGone(R.id.tv_gold, promoteType > 0 && promoteType < 5);
        helper.setGone(R.id.tv_silver, promoteType > 5);
        helper.addOnClickListener(R.id.btn_range_transport);

        PriceBean priceBean = new Gson().fromJson(item.getFast_price_json(), PriceBean.class);
        helper.setText(R.id.tv_express_1to1, TextUtils.isEmpty(priceBean.P1) ? "" : String.format("%s元", priceBean.P1));
        helper.setText(R.id.tv_express_1to3, TextUtils.isEmpty(priceBean.P3) ? "" : String.format("%s元", priceBean.P3));
        helper.setText(R.id.tv_express_1to6, TextUtils.isEmpty(priceBean.P6) ? "" : String.format("%s元", priceBean.P6));
        helper.setText(R.id.tv_express_1to10, TextUtils.isEmpty(priceBean.P10) ? "" : String.format("%s元", priceBean.P10));
        PriceBean priceBean2 = new Gson().fromJson(item.getGeneral_price_json(), PriceBean.class);
        helper.setText(R.id.tv_general_1to1, TextUtils.isEmpty(priceBean2.P1) ? "" : String.format("%s元", priceBean2.P1));
        helper.setText(R.id.tv_general_1to3, TextUtils.isEmpty(priceBean2.P3) ? "" : String.format("%s元", priceBean2.P3));
        helper.setText(R.id.tv_general_1to6, TextUtils.isEmpty(priceBean2.P6) ? "" : String.format("%s元", priceBean2.P6));
        helper.setText(R.id.tv_general_1to10, TextUtils.isEmpty(priceBean2.P10) ? "" : String.format("%s元", priceBean2.P10));
        helper.setText(R.id.tv_genera_aging, String.format("%s-%s天", item.getGeneral_day_min(), item.getGeneral_day_max()));
        helper.setText(R.id.tv_express_aging, String.format("%s-%s天", item.getFast_day_min(), item.getFast_day_max()));
        if (isCompany) {
            helper.addOnClickListener(R.id.tv_line_name);
            helper.addOnClickListener(R.id.iv_status);
            helper.addOnClickListener(R.id.fl_shipping_phone);
            helper.addOnClickListener(R.id.fl_phone_number);
            helper.setGone(R.id.ll_shrink, item.isChecked());
            helper.setImageResource(R.id.iv_status, item.isChecked() ? R.mipmap.ic_zhankai : R.mipmap.ic_shouqi);
            helper.setText(R.id.tv_line_manager, item.getLine_contact());
            helper.setText(R.id.tv_phone_number, item.getLine_mobile());
            if (!MyUtils.isEmpty(item.getOutlets_list())) {
                helper.setText(R.id.tv_shipping_company, item.getOutlets_list().get(0).getOutlets_name());
                helper.setText(R.id.tv_shipping_phone, item.getOutlets_list().get(0).getOutlets_mobile());
                helper.setText(R.id.tv_ship_address, item.getOutlets_list().get(0).getOutlets_address());
            }
            helper.setGone(R.id.ll_quoted_price, item.getPrice_show_status() == 1);
            helper.setText(R.id.tv_principal, item.getPrice_show_status() == 1 ? "运费报价" : "暂无报价");
            helper.setGone(R.id.tv_hint, item.getPrice_show_status() == 1);
        } else {
            helper.setText(R.id.tv_line_manager, String.format("专线经理：%s", MyUtils.getText(item.getLine_contact())));
            helper.setText(R.id.tv_phone_number, String.format("经理手机：%s", MyUtils.getText(item.getLine_mobile())));
            if (!MyUtils.isEmpty(item.getOutlets_list())) {
                helper.setText(R.id.tv_shipping_company, String.format("到站公司：%s", item.getOutlets_list().get(0).getOutlets_name()));
                helper.setText(R.id.tv_shipping_phone, String.format("到站电话：%s", item.getOutlets_list().get(0).getOutlets_mobile()));
                helper.setText(R.id.tv_ship_address, String.format("到站地址：%s", item.getOutlets_list().get(0).getOutlets_address()));
            }
        }

    }
}
